<template>
  <div>
    <h1>新建广告</h1>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="广告">
        <el-button size="small" @click="form.items.push({})">
          <i class="el-icon-plus"></i> 添加新的广告
        </el-button>
        <el-row type="flex" style="flex-wrap: wrap">
          <el-col :md="24" v-for="(item, i) in form.items" :key="i">
            <el-form-item label="跳转链接 (URL)">
              <el-input v-model="item.url"></el-input>
            </el-form-item>
            <el-form-item label="图片" style="margin-top: 0.5rem">
              <el-upload
                class="avatar-uploader"
                :action="$http.defaults.baseURL + '/upload'"
                :show-file-list="false"
                :headers="getAuthHeaders()"
                :on-success="(res) => $set(item, 'image', res.url)"
              >
                <img v-if="item.image" :src="item.image" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>

            <el-form-item>
              <el-button
                size="small"
                type="danger"
                @click="form.items.splice(i, 1)"
                >删除</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form-item>

      <el-button @click="save()">创建</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        name: "",
        icon: "",
        items: [],
        image:''
      },
      // 父级的选项
      parents: [],
    };
  },
  created() {},
  methods: {
    async save() {
      const res = await this.$http.post("/rest/ads", this.form);
      console.log(res);
    },
    afterUpload(res) {
      console.log(res);
      // 显示赋值 vue提供的 明确告诉vue 我们要在form上添加一个icon属性
      this.$set(this.form, "icon", res.url);
      // this.form.icon = res.url
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>